package com.home.jsonhelpers;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.home.constants.Consts;
import com.home.rhclient.MainController;
import com.home.rhtester.R;

import android.content.Context;
import android.provider.Settings.Secure;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class ResourceJsonArrayAdapter extends JsonArrayAdapter {

	public ResourceJsonArrayAdapter(JSONArray jsonArray, Context context, String jsonNameKey, String jsonIdKey, int imageId, int row_layout_style) throws JSONException {
		super(jsonArray, context, jsonNameKey, jsonIdKey, imageId, row_layout_style);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void fillArray(JSONArray jsonArray, String jsonNameKey, String jsonIdKey) throws JSONException {
		for (int i = 0; i < jsonArray.length(); i++) {

			JSONObject obj = jsonArray.getJSONObject(i);
			String name = obj.getString(jsonNameKey);
			String description = obj.getString(Consts.RESOURCE_DESCRIPTION);
			int resourceState = obj.getInt(Consts.RESOURCE_STATE);
			int localResourceState = Consts.RESOURCE_OFF;
			switch (resourceState) {
			case Consts.RESOURCE_NOT_ACTIVE:
				localResourceState = Consts.RESOURCE_OFF;
				break;

			case Consts.RESOURCE_ACTIVE:
				int ownerShip = obj.getInt(Consts.RESOURCE_OWNERSHIP);
				if (ownerShip == Consts.RESOURCE_OWNED) {
					localResourceState = Consts.RESOURCE_ON_OWNED;
				} else {
					localResourceState = Consts.RESOURCE_NOT_OWNER;
				}
				break;
			}
			int id = obj.getInt(jsonIdKey);
			ResourceEntry entry = new ResourceEntry(name, id, description, localResourceState, obj);
			entries.add(entry);
		}
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		View v = arg1;

		if (v == null) {
			ResourceEntry entry = entries.get(arg0);
			LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(row_style, null);
			TextView tvTitle = (TextView) v.findViewById(R.id.row_title);
			TextView tvDescription = (TextView) v.findViewById(R.id.row_description);
			ImageView stateImg = (ImageView) v.findViewById(R.id.row_state_icon);
			ImageView img = (ImageView) v.findViewById(R.id.row_img);
			tvTitle.setText(entry.name);
			tvDescription.setText(entry.description);
			img.setImageResource(imageResource);
			switch (entry.state) {
			case Consts.RESOURCE_OFF:
				stateImg.setImageResource(R.drawable.icon_available2);
				break;
			case Consts.RESOURCE_ON_OWNED:
				stateImg.setImageResource(R.drawable.icon_owned_enabled);
				break;
			case Consts.RESOURCE_ON_NOT_OWNER:
				stateImg.setImageResource(R.drawable.icon_not_available);
				break;

			default:
				stateImg.setImageResource(R.drawable.icon_available2);
				break;
			}
		}
		return v;
	}

}
